using System;
using System.ComponentModel;
using System.Data;
using System.Collections.Generic;

namespace PlacementManager
{
    public partial class UnitComponent
    {

        /// <summary>
        /// Selects all records from the database table for a course.
        /// </summary>
        /// <returns>A collection of objects of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public List<Unit> SelectAllWCourseId(int courseId, string sortExpression)
        {
            List<Unit> listUnit = new List<Unit>();
            PlacementManager.DAL.Unit_DAL o = new PlacementManager.DAL.Unit_DAL();

            DataSet ds = o.SelectAllWCourseID(courseId);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listUnit.Add(new Unit((int)row["UnitId"], (string)row["UnitCode"], (string)row["UnitName"], (string)row["UnitDescription"], (int)row["DivisionId"], (int)row["YearLevel"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]));
            }

            listUnit.Sort(new UnitComparer(sortExpression));
            return listUnit;
        }

        /// <summary>
        /// Selects all records from the database table for a course.
        /// </summary>
        /// <returns>A collection of objects of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<Unit> SelectAllWithoutCourseId(int divisionId, int courseId, string sortExpression)
        {
            List<Unit> listUnit = new List<Unit>();
            PlacementManager.DAL.Unit_DAL o = new PlacementManager.DAL.Unit_DAL();

            DataSet ds = o.SelectAllWithoutCourseId(divisionId, courseId);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listUnit.Add(new Unit((int)row["UnitId"], (string)row["UnitCode"], (string)row["UnitName"], (string)row["UnitDescription"], (int)row["DivisionId"], (int)row["YearLevel"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]));
            }

            listUnit.Sort(new UnitComparer(sortExpression));
            return listUnit;
        }

        /// <summary>
        /// Selects all unist that are associated with a venue for a division.
        /// </summary>
        /// <returns>A collection of objects of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<Unit> SelectAllWVenueIdDivisionId(int venueId, int divisionId, string sortExpression)
        {
            List<Unit> listUnit = new List<Unit>();
            PlacementManager.DAL.Unit_DAL o = new PlacementManager.DAL.Unit_DAL();
            DataSet ds = o.SelectAllWVenueIdDivisionId(venueId, divisionId);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listUnit.Add(new Unit((int)row["UnitId"], (string)row["UnitCode"], (string)row["UnitName"], (string)row["UnitDescription"], (int)row["DivisionId"], (int)row["YearLevel"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]));
            }

            listUnit.Sort(new UnitComparer(sortExpression));
            return listUnit;
        }

        /// <summary>
        /// Selects all units that are not associated with a venue for a division.
        /// </summary>
        /// <returns>A collection of objects of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<Unit> SelectAllWithoutVenueIdDivisionId(int venueId, int divisionId, string sortExpression)
        {
            List<Unit> listUnit = new List<Unit>();
            PlacementManager.DAL.Unit_DAL o = new PlacementManager.DAL.Unit_DAL();
            DataSet ds = o.SelectAllWithoutVenueIdDivisionId(venueId, divisionId);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listUnit.Add(new Unit((int)row["UnitId"], (string)row["UnitCode"], (string)row["UnitName"], (string)row["UnitDescription"], (int)row["DivisionId"], (int)row["YearLevel"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]));
            }

            listUnit.Sort(new UnitComparer(sortExpression));
            return listUnit;
        }

        /// <summary>
        /// Selects all units that are not associated with a venue for a division.
        /// </summary>
        /// <returns>A collection of objects of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<Unit> SelectAllWUserId(int userId, string sortExpression)
        {
            List<Unit> listUnit = new List<Unit>();
            PlacementManager.DAL.Unit_DAL o = new PlacementManager.DAL.Unit_DAL();
            DataSet ds = o.SelectAllWUserId(userId);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listUnit.Add(new Unit((int)row["UnitId"], (string)row["UnitCode"], (string)row["UnitName"], (string)row["UnitDescription"], (int)row["DivisionId"], (int)row["YearLevel"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]));
            }

            listUnit.Sort(new UnitComparer(sortExpression));
            return listUnit;
        }
        
        /// <summary>
        /// Selects one record from the database table using the unit code.
        /// </summary>
        /// <returns>An object of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public Unit SelectOneWUnitCode(string UnitCode)
        {
            PlacementManager.DAL.Unit_DAL o = new PlacementManager.DAL.Unit_DAL();
            o.UnitCode = UnitCode;
            DataTable dt = o.SelectOneWUnitCode().Tables[0];

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                PlacementManager.Unit UnitToReturn = new PlacementManager.Unit((int)row["UnitId"], (string)row["UnitCode"], (string)row["UnitName"], (string)row["UnitDescription"], (int)row["DivisionId"], (int)row["YearLevel"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]);
                return UnitToReturn;
            }
            else
            {
                PlacementManager.Unit UnitToReturnEmpty = new PlacementManager.Unit();
                return UnitToReturnEmpty;
            }
        }

    }
}
